package my.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Customer;
import my.dao.ICustomerDao;
import my.dao.Impl.ICustomerDaoImpl;
import my.service.ICustomerService;

/**
 * 业务实现类,通过调用数据访问层代码,实现数据处理功能
 * 
 * @author slim
 *
 */
public class ICustomerServiceImpl implements ICustomerService {

	//
	private ICustomerDao customerDao = null;

	@Override
	public int add(Customer customer) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			// 连接开启
			conn = DBUtils.getConnection();
			// 开启事务: 设置不自动提交
			conn.setAutoCommit(false);
			// 以数据访问层的实现类实例化数据访问层的接口
			customerDao = new ICustomerDaoImpl(conn);
			// 进行事务处理
			record = customerDao.add(customer);
			// 事务提交
			conn.commit();
		} catch (SQLException e) {
			// 当产生错误时,事务回滚
			System.out.println("出现错误!尝试事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚完毕!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				// 设置回 自动提交
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

		return record;
	}

	@Override
	public int deleteById(int userid) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			customerDao = new ICustomerDaoImpl(conn);
			record = customerDao.deleteById(userid);
			conn.commit();
		} catch (SQLException e) {
			System.out.println("出现错误!尝试事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚完毕!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
		return record;
	}

	@Override
	public int modify(Customer customer) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			customerDao = new ICustomerDaoImpl(conn);
			record = customerDao.modify(customer);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("出现错误!尝试事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚完成!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyPassword(Customer customer) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			customerDao = new ICustomerDaoImpl(conn);
			record = customerDao.modifyPassword(customer);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("出现错误!尝试事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚完成!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Customer> findAll() {
		Connection conn = null;
		List<Customer> list = null;
		try {
			conn = DBUtils.getConnection();
			customerDao = new ICustomerDaoImpl(conn);
			list = customerDao.findAll();
		} finally {
			DBUtils.close(conn, null, null);
		}
		return list;
	}

	@Override
	public Customer findById(int useriId) {
		Connection conn = null;
		Customer customer = null;
		try {
			conn = DBUtils.getConnection();
			customerDao = new ICustomerDaoImpl(conn);
			customer = customerDao.findById(useriId);
			return customer;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Customer findByUserAcount(String accout) {
		Connection conn = null;
		Customer customer = null;
		try {
			conn = DBUtils.getConnection();
			customerDao = new ICustomerDaoImpl(conn);
			customer = customerDao.findByUserAcount(accout);
			return customer;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Customer findByAccountAndPassword(String account, String password) {
		Connection conn = null;
		Customer customer = null;

		try {
			conn = DBUtils.getConnection();
			customerDao = new ICustomerDaoImpl(conn);
			customer = customerDao.findByAccountAndPassword(account, password);
			return customer;
		} finally {
			DBUtils.close(conn, null, null);
		}

	}

}
